import { covertTableData } from '@/utils/tableUtil'
import { request } from '@umijs/max'

export interface PageDataType<V> {
  pages: number
  current: number
  total: number
  data: V[]
}

export class NFBaseService<V, QD, UD> {
  protected _bizName: string = ''
  protected _bizApi: string = '/api'

  getById = async (id: string) => {
    return request<V>(`${this._bizApi}/${this._bizName}?id=${id}`, { method: 'GET' })
  }

  list = async (params?: QD) => {
    return request<V[]>(`${this._bizApi}/${this._bizName}/${this._bizName}List`, { method: 'GET', params })
  }

  page = async (params: QD) => {
    let response = await request<V[]>(`${this._bizApi}/${this._bizName}/${this._bizName}Page`, {
      method: 'GET',
      params,
    })
    return covertTableData(response) as PageDataType<V>
  }

  update = async (data: UD) => {
    return request<V>(`${this._bizApi}/${this._bizName}/edit${this.getBizFistUpper()}`, { method: 'POST', data })
  }

  save = async (data: UD) => {
    return request<V>(`${this._bizApi}/${this._bizName}/add${this.getBizFistUpper()}`, { method: 'POST', data })
  }

  removeById = async (idList: string[]) => {
    return request<boolean>(`${this._bizApi}/${this._bizName}/remove${this.getBizFistUpper()}?idList=${idList}`, {
      method: 'POST',
    })
  }

  getBizFistUpper = () => {
    return this._bizName.slice(0, 1).toUpperCase() + this._bizName.slice(1)
  }
}
